/**
 * jims
 */
package com.jims.master.modules.finance.web;



import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.service.PatVisitService;
import com.jims.master.modules.finance.entity.InpBillDetail;
import com.jims.master.modules.finance.service.InpBillDetailService;
import com.jims.master.modules.finance.service.InpSettleMasterService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 * @author 赵宁
 * @version 2017-03-28
 */
@Controller
@RequestMapping(value = "${adminPath}/finance/inpBillDetail")
public class InpBillDetailController extends BaseController {

	@Autowired
	private InpBillDetailService inpBillDetailService;
	@Autowired
	private InpSettleMasterService inpSettleMasterService;
	@Autowired
	private PatVisitService patVisitService;
	
	@ModelAttribute
	public InpBillDetail get(@RequestParam(required=false) String id) {
		InpBillDetail entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = inpBillDetailService.get(id);
		}
		if (entity == null){
			entity = new InpBillDetail();
		}
		return entity;
	}

	/**
	 * 根据住院号，查询病人费用明细
	 * @param inpBillDetail
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	@ResponseBody
	public List<InpBillDetail> list(InpBillDetail inpBillDetail,String visitNo) {
		PatVisit patVisit= new PatVisit();
		patVisit.setVisitNo(visitNo);
		patVisit = patVisitService.findPatInfoByNo(visitNo, UserUtils.getOrgId());
		inpBillDetail.setOrgId(UserUtils.getOrgId());
		inpBillDetail.setPatVisit(patVisit);
		if(patVisit!=null && patVisit.getId()!=null){
			inpSettleMasterService.outPreferCalcul(visitNo,patVisit.getPatVisitIndex().getChargeType());
		}
		List<InpBillDetail> inpBillDetailList = inpBillDetailService.fingBillDetails(inpBillDetail);
		return inpBillDetailList;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(InpBillDetail inpBillDetail, Model model) {
		model.addAttribute("inpBillDetail", inpBillDetail);
		return "modules/finance/inpBillDetailForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(InpBillDetail inpBillDetail, Model model) {
	    StringData data=new StringData();
		int i=inpBillDetailService.save(inpBillDetail);
        if(i>0){
            data.setCode("success");
            data.setData("保存病人住院费用明细成功");
        }else{
            data.setCode("error");
            data.setData("保存病人住院费用明细失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(InpBillDetail inpBillDetail) {
	    StringData data=new StringData();
		int i=inpBillDetailService.delete(inpBillDetail);
        if(i>0){
            data.setCode("success");
            data.setData("删除病人住院费用明细成功");
        }else{
            data.setCode("error");
            data.setData("删除病人住院费用明细失败");
        }
		return data;
	}

    /**
     * 查询分组退药
     * @param inpBillDetail
     * @param request
     * @param response
     * @param model
     * @return
     * @author zhangyao
     * @date 2017/7/20
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"refundList", ""})
    public String refundList(InpBillDetail inpBillDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<InpBillDetail> list=inpBillDetailService.findListDrugsGroup(inpBillDetail);
        model.addAttribute("list",list);
//        model.addAttribute("patientId",inpBillDetail.getPatientId());
//        model.addAttribute("visitId",inpBillDetail.getVisitId());
        model.addAttribute("inpBillDetail",inpBillDetail);
        return "modules/orders/inpBillDetailRefundList";
    }
}